package com.rk.hosp.receiver;

import com.rk.common.constant.MqConst;
import com.rk.common.service.RabbitService;
import com.rk.hosp.model.hosp.Schedule;
import com.rk.hosp.service.ScheduleService;
import com.rk.hosp.vo.msm.MsmVo;
import com.rk.hosp.vo.order.OrderMqVo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author :Rk.
 * @date : 2022/12/6
 */
@Component
@RabbitListener(queues = MqConst.QUEUE_ORDER)
public class HospitalReceiver {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private RabbitService rabbitService;

    @RabbitHandler
    public void process(OrderMqVo orderMqVo){
        System.out.println("医院收到的消息为:"+orderMqVo);
        System.err.println(orderMqVo.hashCode());
        //可预约数不为空   预约成功
        if(null != orderMqVo.getAvailableNumber()){
            //下单成功更新预约数
            Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
            schedule.setReservedNumber(orderMqVo.getReservedNumber());
            schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
            scheduleService.update(schedule);
        }else{//可预约数为空   取消预约
            //取消预约更新预约数
            //查询出排班信息 可预约数+1
            Schedule schedule = scheduleService.getById(orderMqVo.getScheduleId());
            int availableNumber = schedule.getAvailableNumber().intValue() + 1;
            schedule.setAvailableNumber(availableNumber);
            scheduleService.update(schedule);
        }

        //更新完成后  发送短信
        //发送短信
        MsmVo msmVo = orderMqVo.getMsmVo();
        if(null != msmVo) {
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MESSSAGE, MqConst.ROUTING_EMAIL, msmVo);
        }

    }

}
